package org.jeecg.modules.relation.relbuildingclassroom.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.building.entity.FacBuilding;
import org.jeecg.modules.building.mapper.FacBuildingMapper;
import org.jeecg.modules.building.service.IFacBuildingService;
import org.jeecg.modules.classroom.entity.FacClassroom;
import org.jeecg.modules.classroom.mapper.FacClassroomMapper;
import org.jeecg.modules.relation.relbuildingclassroom.entity.RelBuildingClassroom;
import org.jeecg.modules.relation.relbuildingclassroom.mapper.RelBuildingClassroomMapper;
import org.jeecg.modules.relation.relbuildingclassroom.service.IRelBuildingClassroomService;
import org.jeecg.modules.relation.relbuildingclassroom.vo.RelBuildingClassroomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: rel_building_classroom
 * @author: baochangrong
 * @Date:   2022-02-11
 * @Version: V1.0
 */
@Service
public class RelBuildingClassroomServiceImpl extends ServiceImpl<RelBuildingClassroomMapper, RelBuildingClassroom> implements IRelBuildingClassroomService {

    @Resource
    RelBuildingClassroomMapper relBuildingClassroomMapper;
    @Autowired
    FacBuildingMapper facBuildingMapper;
    @Autowired
    FacClassroomMapper facClassroomMapper;

    /** 通过场地（建筑）id 查询该下的全部教室id*/
    @Override
    public IPage<RelBuildingClassroomVo> listAll(Page<RelBuildingClassroomVo> page, String tenantId){
        //获取该租户下所有建筑id
        List<String> buildingIds = facBuildingMapper.listIds(tenantId);
        List<RelBuildingClassroomVo> relBuildingClassroomVoList = new ArrayList();
        for (String buildingId : buildingIds) {
            List<RelBuildingClassroomVo> relBuildingClassroomVos = relBuildingClassroomMapper.listAll(buildingId);
            for (RelBuildingClassroomVo relBuildingClassroom2 : relBuildingClassroomVos) {
                relBuildingClassroomVoList.add(relBuildingClassroom2);
            }
        }
        Integer total = relBuildingClassroomVoList.size();
        IPage<RelBuildingClassroomVo> result = new Page<>(page.getCurrent(), page.getSize(), total);
        result.setRecords(relBuildingClassroomVoList);
        return result;
    }

    /** 通过教室id 删除场地（建筑）-教室关系表中记录
     * removeByClassroomId
     */
    @Override
    public boolean removeByClassroomId(String classroomId){
        boolean isDelete = relBuildingClassroomMapper.removeByClassroomId(classroomId);
        if (isDelete == true){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<RelBuildingClassroomVo> selectClassroomByBuildingId(String buildingId) {
        return relBuildingClassroomMapper.selectClassroomByBuildingId(buildingId);
    }
}

